//
//  jzRootViewController.m
//  SampleProject
//
//  Created by Jing Zhang on 7/13/14.
//  Copyright (c) 2014 jz. All rights reserved.
//

#import "jzRootViewController.h"
#import "jzFirstViewController.h"
#import "jzSecondViewController.h"
#import "jzThirdViewController.h"
#import "jzMenuViewController.h"

#define MENU_VIEW_WIDTH 240.0f

@interface jzRootViewController ()

@end

@implementation jzRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController.view setFrame:self.view.frame];
    
    jzFirstViewController* vc1 = [[jzFirstViewController alloc] init];
    jzSecondViewController* vc2 = [[jzSecondViewController alloc] init];
    jzThirdViewController* vc3 = [[jzThirdViewController alloc] init];
    
    UINavigationController *tab1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    UINavigationController *tab2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    UINavigationController *tab3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    
    NSArray* controllers = [NSArray arrayWithObjects:tab1, tab2, tab3, nil];
    
    self.tabBarController.viewControllers = controllers;
    
    //set tintcolor, bartintcolor
    self.tabBarController.tabBar.barStyle = UIBarStyleDefault;
    self.tabBarController.tabBar.tintColor = [UIColor blackColor];
    self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tabBarController.view];
    
    UIImage *menuImage = [UIImage imageNamed:@"icon_menu.png"];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:menuImage style:UIBarButtonItemStylePlain target:self action:@selector(openMenu)];
    vc1.navigationItem.leftBarButtonItem = leftButton;
    
    [self addMenuView];
}

- (void)addMenuView {
    
    self.menuVC = [[jzMenuViewController alloc]init];
    //menuVC.delegate = self;
    self.menuVC.isMenuShowing = NO;
    [self.view addSubview:self.menuVC.view];
    self.menuVC.view.frame = CGRectMake(0, 0, MENU_VIEW_WIDTH, self.view.frame.size.height);
    [self.view sendSubviewToBack:self.menuVC.view];
}

- (void)openMenu {
    if (!self.menuVC.isMenuShowing){
        
        [UIView animateWithDuration:0.5f animations:^{
            
            //move view to the right, to show menu
            CGRect frame = self.tabBarController.view.frame;
            self.tabBarController.view.frame = CGRectMake(frame.origin.x + self.menuVC.view.frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
            
            self.tabBarController.view.userInteractionEnabled = NO;
            
            self.menuVC.isMenuShowing = YES;
            
        }];
        
    }
}

- (void)closeMenu{
    
    self.tabBarController.view.userInteractionEnabled = YES;
    
    [UIView animateWithDuration:0.5f animations:^{
        
        CGRect frame = self.tabBarController.view.frame;
        self.tabBarController.view.frame = CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height);
        
    } completion:^(BOOL finished) {
        
        self.menuVC.isMenuShowing = NO;
        
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self closeMenu];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
